package cn.liutao.dish.service;

import cn.liutao.common.utils.ServletUtils;
import cn.liutao.dish.entity.po.DiningTable;
import cn.liutao.dish.mapper.DiningTableMapper;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;

import static cn.liutao.dish.entity.po.table.DiningTableTableDef.DINING_TABLE;

/**
 * 餐桌表 服务层实现。
 *
 * @author liutao
 * @since 2023-11-01
 */
public class DiningTableService {
    private static volatile DiningTableService instance;
    private DiningTableMapper diningTableMapper;
    //私有构造方法
    private DiningTableService(){
        diningTableMapper = ServletUtils.getMapper(DiningTableMapper.class);
    }

    /**
     * 双重检查锁实现单例
     * @return
     */
    public static DiningTableService getInstance(){
        if (instance==null){
            synchronized (DiningTableService.class){
                if (instance==null){
                    instance = new DiningTableService();
                }
            }
        }
        return instance;
    }

    public Page<DiningTable> selectList(DiningTable queryParam) {
        QueryWrapper qw = QueryWrapper.create()
                .where(DINING_TABLE.NAME.like(queryParam.getName()))
                .and(DINING_TABLE.STATUS.eq(queryParam.getStatus()))
                .and(DINING_TABLE.USED_QUANTITY.eq(queryParam.getUsedQuantity()))
                .and(DINING_TABLE.TOTAL_QUANTITY.eq(queryParam.getTotalQuantity()));
        Page<DiningTable> paginate = diningTableMapper.paginate(queryParam.getPageNum(), queryParam.getPageSize(), qw);
        return paginate;
    }

    public void insert(DiningTable addParam) {
        diningTableMapper.insertSelective(addParam);
    }

    public void updateById(DiningTable editParam) {
        diningTableMapper.update(editParam);
    }

    public void removeById(Integer id) {
        diningTableMapper.deleteById(id);
    }

    public DiningTable selectById(Long id){
        DiningTable diningTable = diningTableMapper.selectOneById(id);
        return diningTable;
    }

}
